<template>
  <div class="remark">
    <img :src="'http://43.138.15.137:3000' + props.item.Comment.userAvatar" alt="" @error="userAvatarError">
    <div class="title">
      <!-- 用户名 -->
      <p :class="props.loveTitle === 'remark' ? 'p2' : 'p1'"><span v-show="props.loveTitle === 'remark'">@</span>{{
      props.item.Comment.userNickname }}</p>
      <!-- 介绍 -->
      <p :class="props.loveTitle === 'remark' ? 'p1' : 'p2'">{{ props.item.Comment.commentContent ||
      props.item.Comment.userDesc }}</p>
      <!-- 创建时间 -->
      <p v-show="props.loveTitle === 'remark'">{{ formatTimestamp(props.item.Comment.createdAt) }}</p>
      <!--  爱心 -->
      <Async_love_icon :item="props.item" width="12" @love="love" v-show="props.loveTitle === 'remark'" />
      <!-- 信息 -->
      <Async_message_icon width="341" v-show="props.loveTitle === 'friends'" @message="message" />
    </div>
  </div>
</template>

<script setup>
import { defineAsyncComponent } from 'vue'
const Async_love_icon = defineAsyncComponent(() => import('./icon/loveNum-icon.vue'))
const Async_message_icon = defineAsyncComponent(() => import('./icon/message-icon.vue'))
const props = defineProps({
  item: {
    type: Object
  },
  loveTitle: {
    type: String
  }
})
const emit = defineEmits(['love', 'message'])
// 编码改为年月日
function formatTimestamp(timestamp) {
  let date = new Date(timestamp); // Multiply by 1000 to convert seconds to milliseconds
  let year = JSON.stringify(date).split('-')[0].split('"')
  // console.log(year);
  let monthNames = [
    "1-", "2-", "3-", "4-", "5-", "6-",
    "7-", "8-", "9-", "10-", "11-", "12-"
  ];
  let month = monthNames[date.getMonth()];
  let day = date.getDate() >= 10 ? date.getDate() : "0" + date.getDate();
  return year[1] + '-' + month + day
}
// 爱心
const love = (val) => {
  // console.log(val, 'content');
  let params = val
  emit('love', params)
}
//
const message = () => {
  emit('message')
}
// 头像错误值处理
function userAvatarError(event) {
  // console.log(event.srcElement);
  let img = event.srcElement
  img.src = 'src/assets/avatar/default.png'
  // 防止当原图链接和默认图片链接都失效时，会再次调用失败的方法，就出现了无限循环（闪图）的问题
  img.onerror = null
}
</script>

<style lang="scss" scoped>
img {
  width: 40px;
  height: 40px;
  margin-left: 10px;
  border-radius: 50%;
}

.title {
  display: flex;
  flex-direction: column;
  margin-left: 10px;

  p {
    margin-top: 5px;
  }

  .p2 {
    color: #8b8c96;
  }

  .p1 {
    color: #e8e8e9;
    font-size: 12px;
  }
}

.remark {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  height: 68px;
  font-size: 14px;
  background-color: #161822;
  margin-bottom: 5px;
  padding-left: 10px;
}
</style>